jQuery(document).ready(function($) {
	$("#btnSendMessage").click(function(){
		
		if (ValidateName() & ValidateEmail() & ValidateWeb() & ValidateMsg()){
			var name = EncodeUrl($.trim($("#txtName").val()));
			var email = EncodeUrl($.trim($("#txtEmail").val()));
			var web = EncodeUrl($.trim($("#txtWeb").val()));
			var msg = EncodeUrl($.trim($("#txtMsg").val()));

			$.ajax({
				type: "POST",
				url: $("#hdnTempDirectory").val() + '/ajax-calls.php',
				data: "action=contact&name="+name+"&email="+email+"&web="+web+"&msg="+msg,
				beforeSend: function(xhr) {
					$("#imgLoader").show();
				},
				success: function(data) {
					$("#txtMsg").val('').focus();
					$("#imgLoader").hide();
					SetValidationClass($("#spnMsg"), false, true);
					$.fancybox(data, {});
				},
				error: function() {
					$("#imgLoader").hide();
				}
			});
		}
		
	});
	
	$(".contact-textbox").blur(function(){
		var id = $(this)[0].id;
		switch(id){
			case "txtName":
				ValidateName();break;
			case "txtEmail":
				ValidateEmail();break;
			case "txtWeb":
				ValidateWeb();break;
			case "txtMsg":
				ValidateMsg();break;
		}
	});
	
	function ValidateName(){
		SetValidationClass($("#spnName"), false, true);
		var name = $.trim($("#txtName").val());
		if (name.length < 2){
			SetValidationClass($("#spnName"), false);
			return false;
		}
		else{
			SetValidationClass($("#spnName"), true);
			return true;
		}
	}
	
	function ValidateEmail(){
		SetValidationClass($("#spnEmail"), false, true);
		var email = $.trim($("#txtEmail").val());
		var regex =  /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var isValid = regex.test(email);
		SetValidationClass($("#spnEmail"), isValid);
		return isValid;
	}
	
	function ValidateWeb(){
		SetValidationClass($("#spnWeb"), false, true);
		var web = $.trim($("#txtWeb").val());
		if (web.length > 0){
			var isValid = true;
			//todo - validate url
			SetValidationClass($("#spnWeb"), isValid);
			return isValid;
		}
		
		return true;
	}
	
	function ValidateMsg(){
		SetValidationClass($("#spnMsg"), false, true);
		var msg = $.trim($("#txtMsg").val());
		if (msg.length < 5){
			SetValidationClass($("#spnMsg"), false);
			return false;
		}
		else{
			SetValidationClass($("#spnMsg"), true);
			return true;
		}
	}
	
	function SetValidationClass(element, isValid, reset){
		if (isValid){
			$(element).addClass("contact-valid").removeClass("contact-error");
			$(element).attr("title", "");
			$('#' + element[0].id.replace('spn', 'txt')).removeClass("textbox-error");
		}
		else{
			$(element).addClass("contact-error").removeClass("contact-valid");
			$(element).attr("title", GetErrorMessage(element));
			$('#' + element[0].id.replace('spn', 'txt')).addClass("textbox-error");
		}

		if (reset){
			$(element).removeClass("contact-error").removeClass("contact-valid");
			$(element).attr("title", "");
			$('#' + element[0].id.replace('spn', 'txt')).removeClass("textbox-error");
		}
	}
	
	function GetErrorMessage(element){
		switch(element[0].id.replace("spn", "")){
			case "Name":
				return "Introduceti minimum 3 caractere!";
			case "Email":
				return "Adresa de email nu este valida!";
			case "Web":
				return "Adresa web nu este valida!";
			case "Msg":
				return "Campul Mesaj nu poate fi gol!";
		}
	}
	
	function EncodeUrl(url){
		return encodeURIComponent(url);
	}
});